package com.java.controller;

import com.java.bean.BootstrapTableExpress;
import com.java.bean.Express;
import com.java.bean.Message;
import com.java.bean.ResultData;
import com.java.mvc.Body;
import com.java.service.ExpressService;
import com.java.util.JSONUtil;
import com.java.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

import static com.java.wx.controller.ExpressController.JSONAdd;

@SuppressWarnings("all")
public class ExpressController {
    @Body("/admin/views/map.do")
    public String map(HttpServletRequest req, HttpServletResponse resp) {
        List<Map<String, Object>> map = ExpressService.map();
        if (map == null) {
            return new Message(-1, "无法获取数据").toJSON();
        }
        return new Message(0, map).toJSON();
    }

    @Body("/admin/views/express/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp) {
        try {
            final int id = Integer.parseInt(req.getParameter("id"));
            final String number = req.getParameter("number");
            final String company = req.getParameter("company");
            final String username = req.getParameter("username");
            final String userphone = req.getParameter("userphone");
            final int status = Integer.parseInt(req.getParameter("status"));
            final String send = req.getParameter("sendMessage");
            final String sysphone = UserUtil.getPhone(req.getSession());
            final Express express = new Express(number, company, username, userphone, sysphone);
            express.setEstatus(status);
            if (ExpressService.update(id, express, "true".equals(send))) {
                return JSONUtil.toJSON(new Message(0, "更新成功"));
            }
        } catch (NumberFormatException ignored) {}
        return JSONUtil.toJSON(new Message(-1, "更新失败"));
    }

    @Body("/admin/views/express/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp) {
        final String idString = req.getParameter("id");
        try {
            if (ExpressService.delete(Integer.parseInt(idString)))
                return JSONUtil.toJSON(new Message(0, "删除成功"));
        } catch (NumberFormatException ignored) {}
        return JSONUtil.toJSON(new Message(-1, "删除失败"));
    }

    @Body("/admin/views/express/getByNumber.do")
    public String getByNumber(HttpServletRequest req, HttpServletResponse resp) {
        final String number = req.getParameter("number");
        final Express express = ExpressService.getExpressByNumber(number);
        if (express != null) {
            return JSONUtil.toJSON(new Message(0, "查询成功", new BootstrapTableExpress(express)));
        } else {
            return JSONUtil.toJSON(new Message(-1, "快递单号不存在"));
        }
    }

    @Body("/admin/views/express/add.do")
    public String add(HttpServletRequest req, HttpServletResponse resp) {
        return JSONAdd(req);
    }


    @Body("/admin/views/express/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp) {
        final int offset = Integer.parseInt(req.getParameter("offset"));
        final int pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
        final List<Express> expresses = ExpressService.listExpress(true, offset, pageNumber);
        if (expresses != null) {
            final ResultData<BootstrapTableExpress> expressList = new ResultData<>();
            for (Express express : expresses) {
                expressList.add(new BootstrapTableExpress(express));
            }
            final List<Map<String, Integer>> console = ExpressService.console();
            final int size = console.get(0).get("size");
            expressList.setTotal(size);
            return JSONUtil.toJSON(expressList);
        }
        return null;
    }



}
